<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\User;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\Writer\PngWriter;
use think\Db;
use Endroid\QrCode\QrCode;
use think\Config;
use think\Env;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Color\Color;

class Invitation extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 生成二维码
     */
    public function build()
    {
        $user_id = $this->auth->id;
        if (empty($user_id)) $this->error('请登录');
        $userInfo = User::where('id', $user_id)->field('id,invitation_code')->find();
        $url = Env::get('app.apiDomain') . '?code = ' . $userInfo->invitation_code;
        //  生成二维码
        $writer = new PngWriter();
        $qrCode = QrCode::create($url)
            ->setEncoding(new Encoding('UTF-8'))
            ->setSize(280)
            ->setMargin(2)
            ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));
        $result = $writer->write($qrCode);
        //  二维码保存到目录下
        $code_path = ROOT_PATH . 'public/code/user/' . $user_id . '_code.png';
        $result->saveToFile($code_path);
        //  与背景图合并
        $dst_path = ROOT_PATH . 'public/code/bg.png'; //背景图片路径
        $src_path = $code_path;//覆盖图
        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));
        //获取覆盖图图片的宽高
        list($src_w, $src_h) = getimagesize($src_path);
        //将覆盖图复制到目标图片上，最后个参数100是设置透明度（100是不透明），这里实现不透明效果
        imagecopymerge($dst, $src, 235, 758, 0, 0, $src_w, $src_h, 80);
        //如果覆盖图图片本身带透明色，则使用imagecopy方法
        //imagecopy($dst, $src, 270, 500, 0, 0, $src_w, $src_h);
        //输出图片
        //    list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
        //    switch ($dst_type) {
        //        case 1://GIF
        //            header('Content-Type: image/gif');
        //            imagegif($dst);
        //            break;
        //        case 2://JPG
        //            header('Content-Type: image/jpeg');
        //            imagejpeg($dst);
        //            break;
        //        case 3://PNG
        //            header('Content-Type: image/png');
        //            imagepng($dst);
        //            break;
        //        default:
        //            break;
        //    }
        header("Content-type: image/png");
        $res = imagepng($dst, $code_path);//根据需要生成相应的图片
        imagedestroy($dst);
        imagedestroy($src);
        $returnImg = Env::get('app.yuming') . '/code/user/' . $user_id . '_code.png';
        if ($res) $this->success('获取成功！', $returnImg);
        $this->error('再试一次！');
    }

    /*
     * 前台下载海报
     */

    public function download()
    {
        $file = input('file');
        $file = delDomain($file);
        if (is_file($file)) {
            $file_name = time() . rand(111, 999) . '.jpg';
            //打开文件
            $open_file = fopen($file, "r");
            //输入文件标签
            header("Content-type: application/octet-stream");
            header("Accept-Ranges: bytes");
            header("Accept-Length: " . filesize($file));
            header("Content-Disposition: attachment; filename=" . $file_name);
            ob_clean();
            flush();
            //输出文件内容
            echo fread($open_file, filesize($file));
            fclose($open_file);
            exit();
        }
        return errMsg(400, 'error');
    }
}